/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.staff;

import entity.UserAccessRightsEntity;
import exception.ExistException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.faces.model.SelectItem;
import managedbean.PageRedirectorBean;
import org.primefaces.model.MenuModel;
import session.stateless.StaffAccountSessionBean;
import util.entityhelper.StaffAccountState;
import util.enumeration.AccessRight;
import util.generator.StaffMenuGenerator;
import util.localization.WebLocalization;

@ManagedBean
@RequestScoped
public class StaffEditAccessRightsManagedBean {

    private List<StaffAccountState> staffAccountState = new ArrayList();
    @EJB
    private StaffAccountSessionBean staffAccountMgr;
    private SelectItem[] accessRightSelectItems;
    private UserAccessRightsEntity[] selectedAccessRights;
    private List<AccessRight> accessRightsList;
    private static int num;
    // fields for this bean
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    private PageRedirectorBean redirect;
    // fields passed from session
    private Long staffAccountId;
    private String preferredLocale;
    private MenuModel staffMenuModel;
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private StaffAccountState staffAcctState;

    public StaffEditAccessRightsManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */

        if (ctx.getExternalContext().getSessionMap().containsKey("staffAccountId")) {
            // get session values
            staffAccountId = (Long) ctx.getExternalContext().getSessionMap().get("staffAccountId");
            // put back session values
            ctx.getExternalContext().getSessionMap().put("staffAccountId", staffAccountId);
        } else {
            // redirect back to login
            redirect = new PageRedirectorBean();
            redirect.goStaffLoginPageAtPreviousLevel();
        }
    }

    public void initView(PhaseEvent event) {
        AccessRight[] access = AccessRight.values();
        accessRightSelectItems = new SelectItem[access.length];
        for (int i = 0; i < accessRightSelectItems.length; i++) {
            accessRightSelectItems[i] = new SelectItem(access[i]);
        }

        try {
//            if (staffAccountState == null) {
            staffAccountState = staffAccountMgr.getStaffAccounts();
//            }

            // set language for all MANAGERS for all THROWABLE EXCEPTIONS in backend
            // to use this line everytime possible language changes
            staffAccountMgr.setSelectedLocale(preferredLocale);
            // get data from backend, exception alrd thrown by backend if these methods fail.

            staffAcctState = staffAccountMgr.getStaffAccountByID(staffAccountId);

            // menu
            StaffMenuGenerator menuGenerator = new StaffMenuGenerator(staffAcctState);
            staffMenuModel = menuGenerator.generateStaffMenu();
        } catch (ExistException ex) {
//            System.out.println(ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
//        
    }

    public void confirmChanges(ActionEvent event) {
        for (int i = 0; i < staffAccountState.size(); i++) {
            System.out.println(staffAccountState.get(i).getUsername());
//            staffAccountMgr.updateStaffAccountForSuperAdmin(staffAccountId, staffAccountState.get(i).getAccessRights());
        }
    }

    public List<StaffAccountState> getStaffAccountState() {
        if (num == 0) {
            staffAccountState = new ArrayList();
            List<StaffAccountState> tempList = new ArrayList();
            try {
                tempList = staffAccountMgr.getStaffAccounts();
            } catch (ExistException ex) {
                // TODO: localize
                System.out.println("EXIST ERROR LOR!!");
            }
            System.out.println("Size: " + tempList.size());
            for (int i = 0; i < tempList.size(); i++) {
                StaffAccountState c = (StaffAccountState) tempList.get(i);
                System.out.println("Account ID: " + tempList.get(i).getId());
                System.out.println("Username: " + tempList.get(i).getUsername());
                System.out.println("Creation Date: " + tempList.get(i).getCreationDate());
                System.out.println("AccessRights: " + c.getAccessRights().size());
                StaffAccountState s = new StaffAccountState(tempList.get(i).getId(), tempList.get(i).getUsername(), tempList.get(i).getPassword(), tempList.get(i).getCreationDate(), tempList.get(i).getPasswordChangeDate(), tempList.get(i).getStatus(), c.getAccessRights(), tempList.get(i).getStaff(), tempList.get(i).getUserRoleType());
                s.setAccessRights(c.getAccessRights());
                System.out.println("StateCreated");
                staffAccountState.add(s);
            }
            System.out.println("State Added");
            System.out.println(staffAccountState.size());
        }
        num++;
        return staffAccountState;
    }

    public void setStaffAccountState(List<StaffAccountState> staffAccountState) {

        this.staffAccountState = staffAccountState;
    }

    public UserAccessRightsEntity[] getSelectedAccessRights() {
        return selectedAccessRights;
    }

    public void setSelectedAccessRights(UserAccessRightsEntity[] selectedAccessRights) {
        this.selectedAccessRights = selectedAccessRights;
    }

    public List<AccessRight> getAccessRightsList() {
        return accessRightsList;
    }

    public void setAccessRightsList(List<AccessRight> accessRightsList) {
        this.accessRightsList = accessRightsList;
    }

    /**
     * @return the staffMenuModel
     */
    public MenuModel getStaffMenuModel() {
        return staffMenuModel;
    }

    /**
     * @param staffMenuModel the staffMenuModel to set
     */
    public void setStaffMenuModel(MenuModel staffMenuModel) {
        this.staffMenuModel = staffMenuModel;
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public static int getNum() {
        return num;
    }

    public static void setNum(int num) {
        StaffEditAccessRightsManagedBean.num = num;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public PageRedirectorBean getRedirect() {
        return redirect;
    }

    public void setRedirect(PageRedirectorBean redirect) {
        this.redirect = redirect;
    }

    public Long getStaffAccountId() {
        return staffAccountId;
    }

    public void setStaffAccountId(Long staffAccountId) {
        this.staffAccountId = staffAccountId;
    }

    public StaffAccountSessionBean getStaffAccountMgr() {
        return staffAccountMgr;
    }

    public void setStaffAccountMgr(StaffAccountSessionBean staffAccountMgr) {
        this.staffAccountMgr = staffAccountMgr;
    }

    public StaffAccountState getStaffAcctState() {
        return staffAcctState;
    }

    public void setStaffAcctState(StaffAccountState staffAcctState) {
        this.staffAcctState = staffAcctState;
    }

    public SelectItem[] getAccessRightSelectItems() {
        return accessRightSelectItems;
    }

    public void setAccessRightSelectItems(SelectItem[] accessRightSelectItems) {
        this.accessRightSelectItems = accessRightSelectItems;
    }
}
